package com.ttbj.product.salesman.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ttbj.api.dto.Page;
import com.ttbj.api.dto.RoleDto;
import com.ttbj.api.dto.SalesmanDto;
import com.ttbj.api.exception.BusinessException;
import com.ttbj.api.salesman.RoleService;
import com.ttbj.constant.PlatFormErrorCode;
import com.ttbj.constant.RoleNameEnum;
import com.ttbj.core.util.DtoTransitionUtils;
import com.ttbj.core.util.RegexUtil;
import com.ttbj.salesman.bean.po.Role;
import com.ttbj.salesman.bean.po.Salesman;
import com.ttbj.salesman.dao.RoleMapper;
import com.ttbj.salesman.dao.RolePermissionMapper;
import com.ttbj.salesman.dao.SalesmanMapper;



@Service("roleService")
public class RoleServiceImpl implements RoleService{
	
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private SalesmanMapper salesmanMapper;
	@Autowired
	private RolePermissionMapper rolePermissionMapper;
	
	
	@Override
	public RoleDto querySalemanRoleById(Long salemanId) throws BusinessException {
		Role role = this.roleMapper.querySalemanRoleById(salemanId);
		if (null == role) {
			throw new BusinessException(PlatFormErrorCode.OBJECT_DOES_NOT_EXIST.code());
		}
		return (RoleDto) DtoTransitionUtils.trans(RoleDto.class, role);
	}
	
	@Override
	public List<RoleDto> queryAllSalemanRoleByPage(Page page, SalesmanDto salesmanDto) throws BusinessException {
		System.out.println("salesmanDto.getSalesmanComnum().....:" + salesmanDto.getSalesmanComnum());
		List<Role> roles = this.roleMapper.queryAllSalesmanRoleByPage(page, salesmanDto, RoleNameEnum.COMPANY_ROLE_NAME.rolename());
		List<RoleDto> dtos = new ArrayList<>();
		if (null != roles && !roles.isEmpty()) {
			for (Role role : roles) {
				if (null != role) {
					RoleDto roleDto = (RoleDto) DtoTransitionUtils.trans(RoleDto.class, role);
					dtos.add(roleDto);
				}
			}
		}
		return dtos;
	}
	
	@Override
	public List<RoleDto> queryAllSalemanRole(RoleDto userRoleDto, SalesmanDto salesmanDto) throws BusinessException {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public int queryAllSalemanRoleByPageCount(SalesmanDto salesmanDto) throws BusinessException {
		Salesman salesman = (Salesman) DtoTransitionUtils.trans(Salesman.class, salesmanDto);
		int count = this.roleMapper.queryAllSalesmanRoleByPageCount(salesman, RoleNameEnum.COMPANY_ROLE_NAME.rolename());
		return count;
	}
	
	@Override
	@Transactional
	public void addRole(RoleDto roleDto, SalesmanDto salesmanDto) throws BusinessException {
		if (null == roleDto || null == salesmanDto) {
			throw new BusinessException("对象不存在", PlatFormErrorCode.OBJECT_DOES_NOT_EXIST.code());
		}
		String roleName = roleDto.getRoleName();
		//如果添加的角色名称有root和admin，则throw
		if (roleName.equalsIgnoreCase(RoleNameEnum.SUPER_ROLE_NAME.rolename()) || roleName.equalsIgnoreCase(RoleNameEnum.COMPANY_ROLE_NAME.rolename())) {
			throw new BusinessException("此角色名称已被使用", PlatFormErrorCode.DATA_REPEAT.code());
		}
		//使用正则表达式验证是否为英文字母和数字
		boolean isEl = RegexUtil.isEnglishLetter(roleName);
		if (!isEl) {
			throw new BusinessException("只能填写数字与英文字母", PlatFormErrorCode.DATA_SECURITY.code());
		}
		//获取公司编号
		String comnum = salesmanDto.getSalesmanComnum();
		//获取当前时间
		Date createDate = new Date();
		Role role = (Role) DtoTransitionUtils.trans(Role.class, roleDto);
		//如果不为null， 则说明公司用户登录，增加角色的时候增加公司编号.如果为null，则增加的是超管。
		if (null != comnum) {
			role.setComnum(comnum);
		}
		role.setCreateDate(createDate);
		this.roleMapper.addRole(role);
	}
	
	@Override
	@Transactional
	public void deleteRoleById(Long roleId) throws BusinessException {
		Integer count = this.salesmanMapper.querySalesmanByRoleId(roleId);
		//如果查询出的count >0，则表示有用户，不能删除
		if (count > 0) {
			throw new BusinessException("该角色存在用户，不能删除", PlatFormErrorCode.DATA_REPEAT.code());
		}else {
			//删除角色
			this.roleMapper.deleteRoleById(roleId);
			//删除 角色-权限关系
			this.rolePermissionMapper.deletRolePermissionByID(roleId);
		}
	}
	
	@Override
	@Transactional
	public void updateRoleById(RoleDto roleDto) throws BusinessException {
		if (null == roleDto) {
			throw new BusinessException("对象不存在", PlatFormErrorCode.OBJECT_DOES_NOT_EXIST.code());
		}
		Role role = (Role) DtoTransitionUtils.trans(Role.class, roleDto);
		String roleName = role.getRoleName();
		//如果添加的角色名称有root和admin，则throw
		if (roleName.equalsIgnoreCase(RoleNameEnum.SUPER_ROLE_NAME.rolename()) || roleName.equalsIgnoreCase(RoleNameEnum.COMPANY_ROLE_NAME.rolename())) {
			throw new BusinessException("此角色名称已被使用", PlatFormErrorCode.DATA_REPEAT.code());
		}
		//使用正则表达式验证是否为英文字母和数字
		boolean isEl = RegexUtil.isEnglishLetter(roleName);
		if (!isEl) {
			throw new BusinessException("只能填写数字与英文字母", PlatFormErrorCode.DATA_SECURITY.code());
		}
		this.roleMapper.updateRoleById(role);
	}
	
	@Override
	public RoleDto queryRoleById(long roleId) throws BusinessException {
		Role role = this.roleMapper.queryRoleById(roleId);
		if (null == role) {
			throw new BusinessException("对象不存在", PlatFormErrorCode.OBJECT_DOES_NOT_EXIST.code());
		}
		RoleDto roleDto = (RoleDto) DtoTransitionUtils.trans(RoleDto.class, role);
		return roleDto;
	}
	
	@Override
	public RoleDto queryRoleByName(String rolename) throws BusinessException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<RoleDto> findRolesByComnum(SalesmanDto salesmanDto) throws BusinessException {
		Salesman salesman = (Salesman) DtoTransitionUtils.trans(Salesman.class, salesmanDto);
		String comNum = salesman.getSalesmanComnum();
		List<Role> roles = null;
		//如果公司编号为null，则查询出超管角色
		if (comNum == null) {
			roles = this.roleMapper.queryRolesByComnum(null, null);
		}else {	//如果公司编号为null，则查询出admin角色和公司角色
			roles = this.roleMapper.queryRolesByComnum(comNum, RoleNameEnum.COMPANY_ROLE_NAME.name());
		}
		List<RoleDto> roleDtos = null;
		if (null != roles && !roles.isEmpty()) {
			roleDtos = new ArrayList<>();
			for (Role role : roles) {
				RoleDto rDto = (RoleDto) DtoTransitionUtils.trans(RoleDto.class, role);
				roleDtos.add(rDto);
			}
		}
		return roleDtos;
	}
	
}
